<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>

      <el-form class="login-form" ref="form" :model='form' :rules='rules'>
        <el-form-item prop='mobile'>
          <el-input v-model="form.mobile" placeholder="请输入手机号码"></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>

        <el-form-item prop="agree">
          <el-checkbox v-model="form.agree">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="login" class="login-btn" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reqUserLogin } from '@/api/user.js'
import { setUser } from '@/utils/storage.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '13911111111',
        code: '246810',
        agree: false
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号码', trigger: ['blur', 'change'] },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: ['blur', 'change'] },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码', trigger: ['blur', 'change'] }
        ],
        agree: [
          {
            validator: function (rule, value, callback) {
              if (value === false) {
                callback(new Error('请同意用户体验'))
              } else {
                callback()
              }
            },
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    // ref 和 $refs配合使用可以方便的获取dom元素或者vue组件
    // 1.给需要获取的元素加上ref属性
    // 2.通过this.$refs就可以拿到元素的引用，获取对应的元素
    login () {
      // console.log(this.$refs.form)
      // 先拿到el-form组件，调用内部的校验方法，进行校验
      // 校验通过，才发送ajax请求
      this.$refs.form.validate(flag => {
        // console.log(flag)
        if (flag) {
          reqUserLogin(this.form.mobile, this.form.code).then(res => {
            console.log('登录成功', res)
            this.$message.success('登录成功')
            // localStorage.setItem('token', JSON.stringify(res.data.data))
            setUser(res.data.data)
            this.$router.push('/')
          }).catch(err => {
            this.$message.error('登录失败，手机号或验证码有误')
            console.log(err)
          })
        }
      })
    }
  }
}
</script>

<style lang='less'>
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
